<template>
  <el-dialog title="编辑班级" :visible.sync="dialogVisible" :close-on-click-modal="false"
             :close-on-press-escape="false" :before-close="handleClose" @close="$emit('update:visible',false)">
    <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
      <el-form-item label="班级" prop="name">
        <el-input v-model="editForm.section_name" auto-complete="off" placeholder="填写班级"></el-input>
      </el-form-item>
      <el-form-item label="所属年段" prop="id">
        <el-select v-model="editForm.section_id" placeholder="请选择所属年段">
          <el-option :label="item.section_name" :value="item.id" v-for="(item, index) in arryList" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责老师" prop="name">
        <el-input v-model="editForm.section_name" auto-complete="off" placeholder="选择负责老师"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click.native="resetForm" size="small">取消</el-button>
      <el-button type="primary" @click.native="editSubmit" size="small">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import api from '@/utils/api.js'
  import {pagination, tableoption} from '@/utils/util.js'

  export default {
    name: 'Dialog',
    props: ['visible', 'editId'],
    watch: {
      visible () {
        this.dialogVisible = this.visible
        if (this.visible && this.editId) {
          console.log('编辑', this.editId)
          this.editForm.class_name = this.editId.class_name
          this.editForm.section_id = this.editId.section_id
        }
      }
    },

  data () {
      return {
        dialogVisible: false,
        editForm: {
          section_name: '',
          section_id: ''
        }, // 编辑界面数据
        editFormRules: {
          section_name: [
            {required: true, message: '请填写班级名称', trigger: 'blur'}
          ],
          section_id: [
            {required: true, message: '请选择所属年段', trigger: 'blur'}
          ]
        }, // 表单验证不能为空规则
        arryList: [],
        page: {
          pageindex: pagination.pageIndex,
          pagesize: pagination.pageSize,
          total: pagination.total
        }
      }
  },
    created () {
      this.InitFn()
    },
    methods: {
      InitFn () {
        this.$post(api.admin_section_index, {
          p: this.page.pageindex
        }).then((response) => {
          if (response.code === 0) {
            this.arryList = response.list.data
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      // 提交数据
      editSubmit () {
        // 验证信息
        let _self = this
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            _self.submitBook()
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      submitBook () {
        console.log('原有数据：', this.editId)
        let url_ = api.admin_classes_add
        if (this.editId) {
          url_ = api.admin_classes_edit
          this.editForm.id = this.editId
        }
        this.$post(url_, this.editForm).then((response) => {
          if (response.code === 0) {
            if (!this.editId) {
              this.$confirm('编辑成功是否继续添加', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '返回列表',
                type: 'warning'
              }).then(() => {
                this.$refs.editForm.resetFields()
              }).catch(() => {
                this.$router.go(-1)
                this.$refs.editForm.resetFields()
              })
            } else {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
            }
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      handleClose () {
        this.dialogVisible = false
        this.editForm.name = ''
        this.editForm.id = ''
      },
      resetForm () {
        this.dialogVisible = false
      }
    }
  }
</script>

